import time
import mysql.connector
import pymongo

CONNECTION_STR = 'mongodb://192.168.0.196:27017/?readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=false'
DATA_BASE = 'movies_record'  # 数据库名
DB_COL = 'javbook'  # 表名
SERIES = '10musume'
MYSQL_DB = mysql.connector.connect(
    host="192.168.0.180",
    user="root",
    passwd="123456",
    database="ry-vue"
)


def insert_into_mysql(info):
    db_cursor = MYSQL_DB.cursor()
    sql = (
        "INSERT INTO wuma (num,name,series,years,jav_detail_url,actor,tor,get_time,is_download,download_time,is_save,"
        "is_like,extend_1,extend_2,extend_3,extend_4,extend_5) VALUES ("
        "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
    db_cursor.execute(sql, info)
    MYSQL_DB.commit()
    db_cursor.close()


if __name__ == '__main__':
    # 连接数据库
    db_client = pymongo.MongoClient(CONNECTION_STR)
    db_database = db_client[DATA_BASE]
    db_col = db_database[DB_COL]

    # 查询出全集
    query = {}
    projection = {'_id': 0}
    items = list(db_col.find(query, projection))

    for item in items:
        num = item['num']
        years = item['year']
        series = item['series']
        jav_detail_url = item['detail_url']
        tor = item['tor']

        name, actor, is_download, is_save, is_like, extend_1, extend_2, extend_3, extend_4, extend_5 = '', '', '', '', '', '', '', '', '', ''

        get_time = time.strftime("%Y-%m-%d", time.localtime())
        download_time = get_time

        info = (num, name, series, years, jav_detail_url, actor, tor, get_time, is_download, download_time, is_save,
                is_like, extend_1, extend_2, extend_3, extend_4, extend_5)

        print(info)
        insert_into_mysql(info)

    print('total:{}'.format(len(items)))
